package listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebListener
public class OnlineCountServletContextListener implements ServletContextListener,HttpSessionListener, HttpSessionAttributeListener {
    ServletContext application = null;
    private static Map sessionMap = new HashMap();
    @Override
    public void sessionCreated(HttpSessionEvent se) {

        boolean flag = true;
        for(Object key : sessionMap.keySet()){
            if((String) key == (String)se.getSession().getId()){
                flag = false;
            }
        }
        if(flag){
            sessionMap.put(se.getSession().getId(), se.getSession());
        }
//        会多创建两次，具体原因还未找到，通过-2可以这个问题，这个bug待解决
        System.out.println(sessionMap.size());
        application.setAttribute("numSessions", sessionMap.size()-2);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        boolean flag = false;
        for(Object key : sessionMap.keySet()){
            if((String)key == (String)se.getSession().getId()){
                flag = true;
            }
        }
        if(flag){
            sessionMap.remove(se.getSession().getId());
        }
        application.setAttribute("numSessions", sessionMap.size());
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        List<String> users = new ArrayList<>();
        application =  sce.getServletContext();
        application.setAttribute("onlineUser",users);
        application.setAttribute("onlineUserNumber",0);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent sbe) {
        String user = (String) sbe.getValue();
        HttpSession session = sbe.getSession();
        application  = session.getServletContext();
        List<String> onlineUser = (List<String>) application.getAttribute("onlineUser");
        if(onlineUser.indexOf(user)==-1){
            onlineUser.add(user);
            application.setAttribute("onlineUser",onlineUser);
            application.setAttribute("onlineUserNumber",onlineUser.size());
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent sbe) {
        HttpSession session = sbe.getSession();
        application  = session.getServletContext();
        String user = (String) sbe.getValue();
        List<String> onlineUser = (List<String>) application.getAttribute("onlineUser");
        onlineUser.remove(user);
        application.setAttribute("onlineUser",onlineUser);
        application.setAttribute("onlineUserNumber",onlineUser.size());
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }
}
